package com.kecheng.action;

import com.kecheng.dao.PeizhiDao;
import com.kecheng.pojo.JibieSet;
import com.kecheng.pojo.Peizhi;
import com.kecheng.pojo.TestSet;
import com.kecheng.pojo.Xiaoqu;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 配置的增删改查
 * @author yunxin
 *
 */
@ParentPackage("json-default")
@Action(value = "peizhi", results = {
		@Result(name = "success", location = "/admin/weixin/peizhi.jsp"),
		@Result(name = "timu_success", location = "/admin/admin/timu.jsp"),
		@Result(name = "jibie", location = "/admin/admin/jibieSet.jsp"),
		@Result(name = "xiaoquList", location = "/admin/admin/xiaoqu.jsp"),
		@Result(name = "save", type = "json",params = {"root","object"})
})
@AllowedMethods(value = {("regex:.*")})
public class PeizhiAction extends BaseAction{

	private Peizhi peizhi;
	private TestSet testSet;
	private JibieSet jibieSet;
	private List<Xiaoqu>xiaoquList;
	private Xiaoqu xiaoqu;
	@Autowired
	private PeizhiDao peizhiDao;

	private String msg;

	public String update() {
		peizhiDao.update(peizhi);
		msg = "success";
		peizhi = peizhiDao.find();
		return "success";
	}

	public String find() {
		peizhi = peizhiDao.find();
		return "success";
	}
	/**
	 * 关于题目的设置
	 * @return
	 */
	public String findTimu() {
		 List<TestSet> testSetList= (List<TestSet>)dao.find(TestSet.class);
		testSet = testSetList.get(0);
		return "timu_success";
	}


	/**
	 * 更新题目设置
	 * @return
	 */
	@Transactional
	public String updateTimu() {
		dao.update(testSet);
		msg = "success";
		return findTimu();
	}
	/**
	 * 首测级别评定的设置
	 * @return
	 */
	public String findJibie() {
		List<JibieSet> testSetList= (List<JibieSet>)dao.find(JibieSet.class);
		jibieSet = testSetList.get(0);
		return "jibie";
	}
	/**
	 * 更新 首测级别评定的设置
	 * @return
	 */
	@Transactional
	public String updateJibie() {
		dao.update(jibieSet);
		msg = "success";
		return findJibie();
	}

	/**
	 * 校区列表
	 * @return
	 */
	public String xiaoquList(){
		xiaoquList = (List<Xiaoqu>) dao.find(Xiaoqu.class);
		return "xiaoquList";
	}
	/**
	 * 更新或者添加校区
	 */
	@Transactional(readOnly = false)
	public String save(){
		if(xiaoqu.getId()!=null){
			dao.update(xiaoqu);
		}else{
			dao.add(xiaoqu);
		}
		return "save";
	}

	/**
	 * 删除校区
	 */
	@Transactional(readOnly = false)
	public String deleteXq(){
		dao.delete(xiaoqu);

		return xiaoquList();
	}

	public Peizhi getPeizhi() {
		return peizhi;
	}

	public void setPeizhi(Peizhi peizhi) {
		this.peizhi = peizhi;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public TestSet getTestSet() {
		return testSet;
	}

	public void setTestSet(TestSet testSet) {
		this.testSet = testSet;
	}

	public List<Xiaoqu> getXiaoquList() {
		return xiaoquList;
	}

	public void setXiaoquList(List<Xiaoqu> xiaoquList) {
		this.xiaoquList = xiaoquList;
	}

	public Xiaoqu getXiaoqu() {
		return xiaoqu;
	}

	public void setXiaoqu(Xiaoqu xiaoqu) {
		this.xiaoqu = xiaoqu;
	}

	public JibieSet getJibieSet() {
		return jibieSet;
	}

	public void setJibieSet(JibieSet jibieSet) {
		this.jibieSet = jibieSet;
	}
}
